{% extends 'sys_manage.html' %}
{% block table %}
    {#    {% if cate == '12' %}#}
    <div class="layui-row">
        <div class="layui-col-md6">
            <span>状态：</span>
            <span class="layui-breadcrumb" lay-separator="|" id="status_1">
                  <a href="/user/resource/review/0/" status="0">待审核</a>
                  <a href="/user/resource/review/12/" status="12">已审核</a>
            </span>
        </div>
    </div>
    <table class="layui-table"
           lay-data="{url:'/user/resource/review/1/?status={{ cate }}&new_apply_id={{ new_apply_id }}&review_id={{ review_id }}',text:{none:'暂无相关信息'},limit: 10,page:true,id:'review_table'}"
           lay-filter="review">
        <thead>
        <tr>
            <th lay-data="{field: 'id', width: 80, sort: true, fixed: true}">序号</th>
            <th lay-data="{field: 'apply_user_name',  width: 180}">申请人名称</th>
            <th lay-data="{field: 'apply_user_dir',  width: 180}">申请人所属部门</th>
            <th lay-data="{field: 'apply_time',  width: 180}">申请时间</th>
            {% if cate == '12' %}
                <th lay-data="{field: 'resource_name',  width: 140}">申请资源名称</th>
                <th lay-data="{field: 'status',  width: 103, templet: '#status'}">状态</th>
                <th lay-data="{fixed: 'right',  width: 92, align: 'center', toolbar: '#operate_bar'}">操作</th>
            {% elif cate == '0' %}
                <th lay-data="{field: 'resource_name',  width: 160}">申请资源名称</th>
                <th lay-data="{fixed: 'right',  width: 162, align: 'center', toolbar: '#operate_bar'}">操作</th>
            {% endif %}

        </tr>
        </thead>
    </table>
    <script type="text/html" id="status">
        {% verbatim %}
        {{#  if(d.isdelete == 1){ }}
            <span>资源已删除</span>
        {{#  } else { }}
            {{#  if(d.status == 1){ }}
            <i class="layui-icon" style="color: green" title="通过">&#xe617;</i>
            {{#  } else if(d.status == 2) { }}
            <i class="layui-icon" style="color: red" title="拒绝">&#xe617;</i>
            {{#  } else { }}
            <span>无法获取状态信息</span>
            {{#  } }}
        {{#  } }}
        {% endverbatim %}
    </script>
    <script type="text/html" id="operate_bar">
        {% if cate == '0' %}
            <a class="layui-btn layui-btn-xs" lay-event="pass">通过</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" id="refuse" data-method="refuse" lay-event="refuse"
               data-type="auto">拒绝</a>
        {% elif cate == '12' %}
            {% verbatim %}
            {{#  if(d.status == 1){ }}
            <span class="layui-btn-xs layui-btn layui-btn-disabled">无操作</span>
            {{#  } else if(d.status == 2) { }}
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="refuse_reason">查看原因</a>
            {{#  } else { }}
            <span>无法获取状态信息</span>
            {{#  } }}
            {% endverbatim %}
        {% endif %}
    </script>

    <div class="layui-row" id="refuse_form" style="display: none; padding: 10px">
        <div class="layui-col-md10">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">原因</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入拒绝原因" name="reason" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="refuse_submit">保存</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

{% endblock %}

{% block customjs %}
    {{ block.super }}
    <script>
        $(function () {
            $('#review').addClass('layui-this');
            {% if cate == '0' %}
                $('#status_1 a[status="0"]').addClass('status-this');
            {% else %}
                $('#status_1 a[status="12"]').addClass('status-this');
            {% endif %}
{#            $('#review').parent().addClass('layui-nav-itemed');#}
{#            $('#review a[status="{{ cate }}"]').addClass('layui-this').css({color: '#fff'});#}
        });
        layui.use(['table', 'form'], function () {
            var table = layui.table;
            var form = layui.form;
            var layer = layui.layer;
            var active = {
                // 添加用户按钮点击事件对应的处理方式
                refuse: function (othis) {
                    var type = othis.data('type')
                        , text = othis.text();

                    layer.open({
                        type: 1
                        , title: '填写拒绝原因'
                        , offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
                        , id: 'refuse' + type //防止重复弹出
                        , content: $('#refuse_form')
                        , area: ['600px', '400px']
                        // , btn: ['保存', '取消']
                        {#                        , btnAlign: 'c' //按钮居中#}
                        , shade: 0 //不显示遮罩
                    });
                }
            };

            //监听操作选项的工具条事件
            table.on('tool(review)', function (obj) {
                var data = obj.data;
                if (obj.event === 'refuse_reason') {
                    // 向服务端发送获取原因的请求
                    $.ajax({
                            type: 'get',
                            url: '/user/resource/?real_id=' + data.real_id,
                            dataType: 'json',
                            success: function (data) {
                                if (data.code === 0) {
                                    layer.alert(data.msg);
                                } else {
                                    layer.alert(data.msg)
                                }
                            }
                        }
                    )
                }
                else if (obj.event === 'pass') {
                    //向服务端发送请求，服务器修改此条信息的状态
                    $.ajax({
                            type: 'put',
                            url: '/user/resource/',
                            data: {real_id: data.real_id, status: '1'},
                            dataType: 'json',
                            success: function (data) {
                                if (data.code === 0) {
                                    layer.msg(data.msg, {
                                        icon: 6, time: 2000, end: function () {
                                            location.href = '/user/resource/review/0/'
                                        }
                                    });
                                } else {
                                    layer.alert(data.msg)
                                }
                            }
                        }
                    )

                } else if (obj.event === 'refuse') {

                    var othis = $(this), method = othis.data('method'),
                        $real_id = '<input type="hidden" name="real_id" value="' + data.real_id + '">';
                    $('#refuse_form form').append($real_id);
                    active[method] ? active[method].call(this, othis) : '';
                }
            });
            // 监听填写拒绝信息后保存
            form.on('submit(refuse_submit)', function (data) {
                var $data = data.field;
                $data['status'] = '2';
                $.ajax({
                        type: 'put',
                        url: '/user/resource/',
                        data: $data,
                        dataType: 'json',
                        success: function (data) {
                            if (data.code === 0) {
                                {#                                alert(data.msg)#}
                                layer.msg(data.msg, {
                                    icon: 6, time: 2000, end: function () {
                                        location.reload()
                                    }
                                });
                            } else {
                                layer.alert('Failed:' + data.msg);
                                return
                            }
                        }
                    }
                );
                return false;
            });

        });
    </script>
{% endblock %}
